package com.zhongwang.business.module.estimateBudgetAccountingManage.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.*;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingInfoDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.AccHeatTreatmentImportModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.AccMachiningHoursImportModel;
import com.zhongwang.business.util.BeanUtil;
import com.zhongwang.business.util.ShiroUtils;
import com.zhongwang.business.util.StringUtils;

import java.util.*;

/**
 * @author 徐一贺
 * @className AccMachiningHoursImportListener
 * @description 机加信息息导入监听器
 * @date 2020/11/21 14:46
 * @versron ver 1.0
 */
public class AccMachiningHoursImportListener extends AnalysisEventListener<AccMachiningHoursImportModel> {
    /**
     * 钩子
     */
    private ImportCallBack callBack;
    /**
     * Excel相关参数模型
     */
    private BusMachiningHours param;
    /**
     * 材料信息数据持久层
     */
    private AccountingInfoDao accountingInfoDao;
    /**
     * 错误列表
     */
    private List<AccMachiningHoursImportModel> errorList = new ArrayList<>();
    /**
     * 持久化列表
     */
    private List<AccMachiningHoursImportModel> persistenceList = new ArrayList<>();
    /**
     * 当前时间
     */
    private Date currentTime = new Date();
    /**
     * 专业部分信息
     */
    private Map<String, List<String>> majorParts;
    /**
     * 设备型号列表
     */
    private List<String> equipmentModels;

    /**
     * 构造函数
     *
     * @param param             页面参数
     * @param callBack          回调函数
     * @param accountingInfoDao 持久层
     */
    public AccMachiningHoursImportListener(
            BusMachiningHours param, ImportCallBack callBack, AccountingInfoDao accountingInfoDao) {
        this.callBack = callBack;
        this.param = param;
        this.accountingInfoDao = accountingInfoDao;
        majorParts = new HashMap<>();
        BusBudgetAccountingCost findPartListParam = new BusBudgetAccountingCost();
        findPartListParam.setBatId(param.getBatId());
        findPartListParam.setMajor("机械");
        List<String> mechanicsPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
        findPartListParam.setMajor("流体");
        List<String> fluidPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
        findPartListParam.setMajor("自动化");
        List<String> automationPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
        majorParts.put("机械", mechanicsPart);
        majorParts.put("流体", fluidPart);
        majorParts.put("自动化", automationPart);
        equipmentModels = accountingInfoDao.findEquipmentModelsByBatId(param.getBatId());
    }

    /**
     * 验证
     *
     * @param data 每行数据
     */
    @Override
    public void invoke(AccMachiningHoursImportModel data, AnalysisContext context) {
        String errorInfo = "";
        boolean isRightMajor = false;
        if (data.getMajor() == null || StringUtils.trimAllBlank(data.getMajor()).equals("")) {
            errorInfo = "请填写专业！\r\n";
        } else if (data.getMajor() != null && data.getMajor().length() > 255) {
            errorInfo = "专业填写不正确，正确的是机械、流体或自动化！\r\n";
        }
        if (data.getMajor().equals("机械")
                || data.getMajor().equals("流体")
                || data.getMajor().equals("自动化")) {
            isRightMajor = true;
        } else {
            errorInfo = "专业填写不正确，正确的是机械、流体或自动化！\r\n";
        }
        if (data.getPart() == null || StringUtils.trimAllBlank(data.getPart()).equals("")) {
            errorInfo = "填写部分！\r\n";
        } else if (data.getPart() != null && data.getPart().length() > 255) {
            errorInfo = "部分不可超过255个字符！\r\n";
        } else if (data.getPart() != null && data.getMajor() != null && isRightMajor) {
            List<String> parts = majorParts.get(data.getMajor());
            if (!parts.contains(data.getPart())) {
                errorInfo = "部分填写错误，该专业下无此部分！\r\n";
            }
        }
        if (data.getDiagram() == null || StringUtils.trimAllBlank(data.getDiagram()).equals("")) {
            errorInfo = "请填写图号！\r\n";
        }else if (data.getDiagram() != null && data.getDiagram().length() > 255) {
            errorInfo = "图号不可超过255个字符！\r\n";
        }

        if (data.getDrawingNo() == null || StringUtils.trimAllBlank(data.getDrawingNo()).equals("")) {
            errorInfo = "请填写图纸名称！\r\n";
        }else if (data.getDrawingNo() != null && data.getDiagram().length() > 255) {
            errorInfo = "图纸名称不可超过255个字符！\r\n";
        }

        if (data.getProcess() == null || StringUtils.trimAllBlank(data.getProcess()).equals("")) {
            errorInfo = "请填写工序！\r\n";
        }else if (data.getProcess() != null && data.getProcess().length() > 255) {
            errorInfo = "工序不可超过255个字符！\r\n";
        }
        if (data.getName() == null || StringUtils.trimAllBlank(data.getName()).equals("")) {
            errorInfo = "请填写设备型号！\r\n";
        }else if (data.getName() != null && data.getName().length() > 255) {
            errorInfo = "设备型号不可超过255个字符！\r\n";
        }else if(!equipmentModels.contains(data.getName())){
            errorInfo = "设备型号不存在，如果改设备型号存在于《技术文件系统》中请在基础信息配置中维护该核算选定版本的机加定额数据！\r\n";
        }
        if (data.getHours() == null) {
            errorInfo = "请填写工时！\r\n";
        }
        if (errorInfo.length() > 0) {
            data.setErrorInfo(errorInfo);
            errorList.add(data);
        } else {
            persistenceList.add(data);
        }
    }

    /**
     * 持久化
     *
     * @param context
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (errorList.size() > 0) {
            callBack.onWarning(errorList);
        } else {
            Date currentDate = new Date();
            BusBudgetAccountingTask order =
                    accountingInfoDao.getBusBudgetAccountingTaskModelById(param.getBatId());
            for (int i = 0; i < persistenceList.size(); i++) {
                AccMachiningHoursImportModel persistenceData = persistenceList.get(i);
                persistenceData.setBatId(param.getBatId());
                persistenceData.setDataUseType(2);
                persistenceData.setGetDate(currentDate);
                persistenceData.setGetUid(ShiroUtils.getUserId());
                persistenceData.setType(2);
                persistenceData.setProCode(order.getProCode());
                BusBudgetAccountingCost setCost = null;
                BusBudgetAccountingCost getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
                getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
                getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
                getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
                getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-机加");
                setCost = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
                persistenceData.setCostId(setCost.getId());
                BusMachiningHours data = new BusMachiningHours();
                BeanUtil.copy(persistenceData, data);
                accountingInfoDao.insertBusMachiningHours(data);
                setCost.setDataSource("1");
                accountingInfoDao.updateBusBudgetAccountingCostDataSourceByPrimaryKey(setCost);
            }
            callBack.onSuccess();
        }
    }

    /**
     * 异常处理
     *
     * @param exception 异常信息
     * @param context
     */
    @Override
    public void onException(Exception exception, AnalysisContext context) {
    }
}
